package order

import (
	"errors"
	config "pd/server/app/pd/config"
	form "pd/server/app/pd/form/order"
	logic "pd/server/app/pd/logic/order"
	userLogic "pd/server/app/pd/logic/user"
	model "pd/server/app/pd/model/order"
	"pd/server/plugin/api"
	"pd/server/plugin/auth"
	"pd/server/plugin/logger"

	"github.com/gin-gonic/gin"
)

var orderLogic logic.OrderLogic

// 新增订单
func Create(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/add", func(c *gin.Context) {
		var frm = new(form.CreateOrderForm)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}
		if frm.Nickname == "" {
			api.No(c, errors.New("客户昵称不能为空"))
			return
		}
		if frm.Address == "" {
			api.No(c, errors.New("派送地址不能为空"))
			return
		}
		if frm.PhoneNumber == "" {
			api.No(c, errors.New("客户手机号不能为空"))
			return
		}

		authInfo := auth.GetAuthInfo(c)
		if frm.MerchantId, err = userLogic.GetUserMerchantId(c, authInfo.UserId); err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		err = orderLogic.Create(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, frm)
	})
}

// 更新订单
func Update(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/edit", func(c *gin.Context) {
		var frm = new(form.CreateOrderForm)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}
		if frm.Nickname == "" {
			api.No(c, errors.New("客户昵称不能为空"))
			return
		}
		if frm.Address == "" {
			api.No(c, errors.New("派送地址不能为空"))
			return
		}
		if frm.PhoneNumber == "" {
			api.No(c, errors.New("客户手机号不能为空"))
			return
		}

		authInfo := auth.GetAuthInfo(c)
		if frm.MerchantId, err = userLogic.GetUserMerchantId(c, authInfo.UserId); err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		err = orderLogic.Update(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, frm)
	})
}

func Delete(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/remove", func(c *gin.Context) {
		var frm = new(model.OrderModel)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}

		authInfo := auth.GetAuthInfo(c)
		if frm.MerchantId, err = userLogic.GetUserMerchantId(c, authInfo.UserId); err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		err = orderLogic.Delete(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, frm)
	})
}

func OrderList(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/page", func(c *gin.Context) {
		var frm = new(form.OrderListForm)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}

		authInfo := auth.GetAuthInfo(c)
		if frm.MerchantId, err = userLogic.GetUserMerchantId(c, authInfo.UserId); err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		orderList, total, err := orderLogic.OrderList(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, gin.H{
			"total":   total,
			"current": frm.Current,
			"size":    frm.Size,
			"records": orderList,
		})
	})
}
